<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/12/21
 * Time: 15:36
 */
namespace app\admin\controller;
use think\Db;
class Recovery extends Base
{
    /*
     * 用户回收价格
     */
    public function userPrice(){
        if(request()->isAjax ()){
            extract(input());
            $map['rec_status'] = 1;
            if(isset($key)&&$key!="")
            {
                $map['rec_name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('recovery')->where($map)->count();
            $lists = Db::name('recovery')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('rec_create_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('recovery/user_price');
    }

    /*
     * 添加用户回收价格
     */
    public function userPriceAdd(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'rec_name'=>$name,
                'rec_money'=>$money,
                'rec_create_time'=>$time,
                'rec_update_time'=>$time
            ];
            $res = Db::name('recovery')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch ('recovery/user_price_add');
    }

    /*
     * 编辑用户回收价格
     */
    public function userPriceEdit(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'rec_name'=>$name,
                'rec_money'=>$money,
                'rec_update_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('recovery')->where('rec_id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $info = Db::name('recovery')->where('rec_id',input('id'))->find();
        return $this->fetch('recovery/user_price_edit',['info'=>$info]);
    }

    /*
     * 删除用户回收价格
     */
    public function userPriceDel(){
        $res = Db::name('recovery')->where('rec_id',input('id'))->setField ('rec_status',2);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /*
     * 代理回收价格
     */
    public function agencyPrice(){
        if(request()->isAjax ()){
            extract(input());
            $map['status'] = 1;
            if(isset($key)&&$key!="")
            {
                $map['name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('daili_rec')->where($map)->count();
            $lists = Db::name('daili_rec')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('create_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('recovery/agency_price');
    }

    /*
     * 添加代理回收价格
     */
    public function agencyPriceAdd(){
        if(request()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'name'=>$name,
                'money'=>$money,
                'more_money'=>$more_money,
                'create_time'=>$time,
                'update_time'=>$time
            ];
            $res = Db::name('daili_rec')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch ('recovery/agency_price_add');
    }

    /*
     * 编辑代理回收价格
     */
    public function agencyPriceEdit(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'name'=>$name,
                'money'=>$money,
                'more_money'=>$more_money,
                'update_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('daili_rec')->where('id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $info = Db::name('daili_rec')->where('id',input('id'))->find();
        return $this->fetch ('recovery/agency_price_edit',['info'=>$info]);
    }

    /*
     * 删除代理回收价格
     */
    public function agencyPriceDel(){
        $res = Db::name('daili_rec')->where('id',input('id'))->setField ('status',2);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }

    /*
     * 每日铅价
     */
    public function dayPrice(){
        if(request()->isAjax ()){
            extract(input());
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('set')->where('id',1)->count();
            $lists = Db::name('set')
                ->where('id',1)
                ->page($Nowpage,$limits)
                ->order('create_time desc')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch ('recovery/day_price');
    }

    /*
     * 编辑每日铅价
     */
    public function dayPriceEdit(){
        if(request()->isPost ()){
            extract(input());
            $param = [
                'price'=>$price,
                'butie'=>$butie,
                'update_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('set')->where('id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $info = Db::name('set')->where('id',1)->find();
        return $this->fetch ('recovery/day_price_edit',['info'=>$info]);
    }
}